package cn.edu.tju.cas.dao.impl;

import cn.edu.tju.cas.dao.UserDao;
import cn.edu.tju.cas.entity.UserEntity;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by catold on 16/2/20.
 */
@Repository("userDao")
public class UserDaoImpl extends BaseDaoImpl<UserEntity> implements UserDao{

    public UserDaoImpl(){
        super();

        setClass(UserEntity.class);
    }

    public UserEntity findByUsername(String username) {
        return getOneFromList(getHibernateTemplate().find("from UserEntity where username=?", username));
    }

    // 问题是此处查到的没有角色信息，因为没有关联
    @Override
    public List<UserEntity> findByAdminType(int isAdmin) {
        return (List<UserEntity>)getHibernateTemplate().find("from UserEntity where isAdmin=?", isAdmin);
    }

    @Override
    public UserEntity finByIdCard(String idCard) {
        return getOneFromList(getHibernateTemplate().find("from UserEntity where idCard=?", idCard));
    }

    @Override
    public List<Integer> findUserIds() {
        return (List<Integer>)getHibernateTemplate().find("select userId from UserEntity");
    }

}
